package com.qcby.controller;

import com.qcby.common.Result;
import com.qcby.form.LoginForm;
import com.qcby.form.RegisterForm;
import com.qcby.form.UpdatePasswordForm;
import com.qcby.form.UserUpdateForm;
import com.qcby.service.TUserService;
import com.qcby.vo.LoginVO;
import com.qcby.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@RestController
@RequestMapping("/user")
public class TUserController {

    @Autowired
    private TUserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Valid RegisterForm form) {
        userService.register(form);
        return Result.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody @Valid LoginForm form) {
        LoginVO loginVO = userService.login(form);
        return Result.success(loginVO);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<UserInfoVO> getUserInfo() {
        UserInfoVO userInfo = userService.getCurrentUserInfo();
        return Result.success(userInfo);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody @Valid UserUpdateForm form) {
        userService.updateUserInfo(form);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result updatePassword(@RequestBody @Valid UpdatePasswordForm form) {
        userService.updatePassword(form);
        return Result.success();
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result logout() {
        userService.logout();
        return Result.success();
    }
}

